//
//  Seach_FiltrateDialogs_View_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/31.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Seach_FiltrateDialogs_View_WX.h"

@interface Seach_FiltrateDialogs_View_WX()
@property (nonatomic, strong) UIView *dimBackgroundView;/**< 半透明黑色背景 */
@property (nonatomic, strong) UIWindow *window;

@end


@implementation Seach_FiltrateDialogs_View_WX


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}





/* MARK: ================= custom =================*/
-(Seach_SearchResultMain_View_WX *)mainView{
    if (!_mainView) {
        _mainView = kUSER_SearchResultMain_MANAGER;
   
        @weakify(self);
        [[self.mainView.NavView.cancelBtn addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            [self hide];
        }];
        [[self.mainView.NavView.emptyBtn addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            kUSER_Filtrate_MANAGER.SearchResultTopBarModel.keyWord = @"筛选";
            self.mainView.NavView.emptyBtn.hidden = YES;
        }];

    }
    return _mainView;
}


- (UIView *)dimBackgroundView
{
    if (!_dimBackgroundView) {
        _dimBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0 )];
        _dimBackgroundView.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        _dimBackgroundView.backgroundColor = [UIColor blackColor];
        _dimBackgroundView.alpha = 0;
        
        // 添加手势监听
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [_dimBackgroundView addGestureRecognizer:tap];
    }
    return _dimBackgroundView;
}


- (void)show
{
    [self addToKeyWindow];
    [self showAnimationWithCompletion:nil];
    [self.mainView.BottomV refload];
}

- (void)hide
{
    [self hideAnimationWithCompletion:^(BOOL finished) {
        [self removeFromKeyWindow];
    }];
}


#pragma mark - private method

- (void)addToKeyWindow
{
    if (!self.superview) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow addSubview:self];
        
        [self addSubview:self.dimBackgroundView];
        [self addSubview:self.mainView];

//        [self addSubview:self.LoginSetPasswordV];
//        [self.LoginSetPasswordV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self).offset(30);
//            make.right.equalTo(self).offset(-30);
//            make.centerY.equalTo(self.mas_centerY).offset(0);
//            make.height.mas_equalTo((kScreenW-60)/319*351);
//        }];
    }
}

- (void)removeFromKeyWindow
{
    if (self.superview) {
        [self removeFromSuperview];
    }
}

- (void)showAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0.3;
    } completion:completion];
}

- (void)hideAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0;
    } completion:completion];
}


@end
